package com.cyys.modules.core.sysParam;

import com.cyys.common.redis.RedisUtil;
import com.cyys.modules.system.model.org.Org;
import com.cyys.modules.system.service.org.OrgService;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class OrgUtil {

    public static void init(OrgService orgService) {
        if(RedisUtil.hSize(RedisConstants.ORG_MAP) >= 0){
            RedisUtil.delete(RedisConstants.ORG_MAP);
        }
        List<Org> list = orgService.listAll(null);
        for (Org entry : list) {
            RedisUtil.hPut(RedisConstants.ORG_MAP,entry.getId(),entry);
        }
    }

    public static Map<Object, Object> getAll() {
        return RedisUtil.hGetAll(RedisConstants.ORG_MAP);
    }

    public static Org get(String orgId) {
        return (Org) RedisUtil.hGet(RedisConstants.ORG_MAP, orgId);
    }

    public static void add(Org org) {
        RedisUtil.hPut(RedisConstants.ORG_MAP,org.getId(),org);
    }

    public static void delete(String orgId) {
        RedisUtil.hDelete(RedisConstants.ORG_MAP,orgId);
    }

    public static void modify(Org org) {
        RedisUtil.hDelete(RedisConstants.ORG_MAP,org.getId());
        RedisUtil.hPut(RedisConstants.ORG_MAP,org.getId(),org);
    }
}
